package com.wty.cmims.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wty.cmims.pojo.Appointment;
import com.wty.cmims.pojo.Code;
import com.wty.cmims.pojo.ResponseResult;
import com.wty.cmims.pojo.Schedule;
import com.wty.cmims.service.AppointmentService;
import com.wty.cmims.mapper.AppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author wty
* @description 针对表【appointment】的数据库操作Service实现
* @createDate 2024-10-28 13:23:37
*/
@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment>
    implements AppointmentService{
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private ScheduleServiceImpl scheduleService;
    @Override
    @Transactional
    public ResponseResult<Map<String,Object>> appointmentAdd(Map<String, Object> paramsMap) {
        ResponseResult<Map<String,Object>> responseResult = new ResponseResult<>();
        Appointment appointment = new Appointment();
        appointment.setResidentId(Integer.parseInt(paramsMap.get("resident_id").toString()));
        appointment.setDoctorId(Integer.parseInt(paramsMap.get("doctor_id").toString()));
        appointment.setInstitutionId(Integer.parseInt(paramsMap.get("institution_id").toString()));
        appointment.setScheduleId(Integer.parseInt(paramsMap.get("schedule_id").toString()));

        int rows = 0;
//        // 如果存在一条记录则修改已有记录
//        LambdaQueryWrapper<Appointment> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Appointment::getResidentId, appointment.getResidentId())
//                .eq(Appointment::getScheduleId, appointment.getScheduleId());
//        Appointment appointmentExist = this.getOne(queryWrapper);
//        Map<String, Object> appointmentId = new HashMap<>();
//        if (appointmentExist != null){
//            rows = appointmentMapper.updateById(appointmentExist);
//        }else{
//            // 否则新增一条预约记录
            rows = appointmentMapper.insert(appointment);
//        }
        Map<String, Object> appointmentId = new HashMap<>();
        appointmentId.put("appointment_id", appointment.getId());
        if (rows > 0){
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("已申请预约，待后台确认");
            responseResult.setData(appointmentId);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("申请预约失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> appointmentStatusFind(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        List<Map<String, Object>> appointments = appointmentMapper.findAppointmentStatus(paramsMap);
        if (appointments != null){
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询预约信息成功");
            responseResult.setDatas(appointments);
        } else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("查询预约信息失败");
        }
        return responseResult;
    }

    @Override
    @Transactional
    public ResponseResult<Void> appointmentCancel(Map<String,Object> paramsMap) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<Appointment> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Appointment::getId, paramsMap.get("id"))
                .set(Appointment::getStatus, 4);
        boolean updateAppointmentStatus = this.update(wrapper);
        // 使排班表对应排班预约号次加一
        LambdaUpdateWrapper<Schedule> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Schedule::getId, paramsMap.get("schedule_id"))
                .setSql("rest = rest + 1");
        boolean updateSchedule = scheduleService.update(updateWrapper);
        if (updateAppointmentStatus && updateSchedule){
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("取消预约成功");
        } else{
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("取消预约失败");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Map<String, Object>> appointmentRecordsFind(Map<String, Object> paramsMap) {
        ResponseResult<Map<String, Object>> responseResult = new ResponseResult<>();
        Integer total = 0;
        Integer pageSize = (Integer) paramsMap.get("pageSize");
        paramsMap.put("start", ((Integer) paramsMap.get("currentPage") - 1) * pageSize);
        total = appointmentMapper.findAppointmentsRecordNum(paramsMap);
        if (total != null && total < pageSize){
            paramsMap.put("start", 0);
            paramsMap.put("currentPage", 1);
        }
        List<Map<String, Object>> appointments = appointmentMapper.findAppointmentsRecord(paramsMap);
        if (!appointments.isEmpty()){
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("查询预约记录成功");
            responseResult.setDatas(appointments);
            responseResult.setTotal(total);
            responseResult.setCurrentPage((Integer) paramsMap.get("currentPage"));
        }else {
            responseResult.setCode(Code.ERROR);
            responseResult.setMessage("未找到相关预约记录");
        }
        return responseResult;
    }

    @Override
    public ResponseResult<Void> appointmentPayUpdate(Integer id, String tradeNo) {
        ResponseResult<Void> responseResult = new ResponseResult<>();
        LambdaUpdateWrapper<Appointment> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Appointment::getId, id)
                .set(Appointment::getStatus, 0)
                .set(Appointment::getIsPay, 1)
                .set(Appointment::getTradeNo, tradeNo);
        boolean update = this.update(wrapper);
        if (update){
            responseResult.setCode(Code.SUCCESS);
            responseResult.setMessage("支付成功");
        } else {
            responseResult.setCode(Code.ERROR);
        }
        return responseResult;
    }
}